﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Models;
namespace LX.Controllers
{
    public class DefaultController : Controller
    {
        // GET: Default
        public ActionResult Index()
        {
            Model1 model = new Model1();
            var list = model.Task.ToList();
            return View(list);
        }
        public ActionResult tianjia()
        {
            return View();
        }
        [HttpPost]
        public ActionResult tianjia(Task te)
        {
            Model1 mo = new Model1();
            te.AddTime = DateTime.Now;
            te.TaskState = 1;
            te.FinishedTime = null;
            mo.Task.Add(te);
            var res = mo.SaveChanges();
            if (res > 0)
            {
                return RedirectToAction("Index");
            }
            return View(te);
        }
        public ActionResult xiugai(int? te) {
            //实例化
            Model1 mo = new Model1();
            if (te == null) { 
            return RedirectToAction("index");
            }
            //将值传递给前台
            var task = mo.Task.FirstOrDefault(p => p.TaskID == te);
            //将值返回
            return View(task);
        }
        [HttpPost]
        public ActionResult xiugai(Task task)
        {
            Model1 mo = new Model1();
            if (ModelState.IsValid) { 
            var temp=mo.Task.FirstOrDefault(p => p.TaskID == task.TaskID);
                if (temp != null)
                {
                    temp.TaskPriority = task.TaskPriority;
                    temp.Content = task.Content;
                    temp.uid = task.uid;
                    var res = mo.SaveChanges();
                    if (res > 0) {
                        return RedirectToAction("index");
                    }
                }
            }
            return View();
        }
        public ActionResult shanchu(int te) {
            Model1 mo = new Model1();
            var task = mo.Task.FirstOrDefault(p => p.TaskID == te);
            if (task != null)
            {
                mo.Task.Remove(task);
                var returns = mo.SaveChanges();
                return Json(returns);
            }
            return Json(false);
        }
        public ActionResult xiangqing(int te) {
            Model1 mo = new Model1();
            var task = mo.Task.FirstOrDefault(p => p.TaskID == te);

            return View(task);
        }
    }
}